package cn.xi.controller;

import cn.xi.checkItem.OrdersettingService;
import cn.xi.constant.MessageConstant;
import cn.xi.entity.Result;
import cn.xi.pojo.OrderSetting;
import cn.xi.utils.POIUtils;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;

    @RequestMapping("/upload")
    public Result orderAdd(@RequestParam("excelFile")MultipartFile excelFile){
        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);
                  List<OrderSetting> list =new ArrayList<>();
            for (String[] string : strings) {
                OrderSetting orderSetting =
                        new OrderSetting(new Date(string[0]), Integer.parseInt(string[1]));
                list.add(orderSetting);
            }
            ordersettingService.add(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

@RequestMapping("/select")
    public List<Map> select(String date){
    List<Map> select = ordersettingService.select(date);
    return select;
}

    @RequestMapping("/updatedate")
    public Result updatedate(@RequestBody Date date, String number) {
        try {
            ordersettingService.update(date,number);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
        return  new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}
